


var app= angular.module('user_5App', []);
app.controller('user_5Controller', function($scope) {
	$scope.id=888;
    var roles=['mamaster,'+$scope.id,'proposer,'+$scope.id,'involver,'+$scope.id,'recorder,'+$scope.id];
	var connections={};
	var role='involver,'+$scope.id;
	var socket = new WebSocket("ws://" + window.location.host  +"/webSocket/meeting/" + role);
	var memberin;
	

	
	//处理到来的信令
	socket.onmessage = function(event) {
		var json = JSON.parse(event.data);
		if(json.proCloseOwn!=null){
			//自己关闭申请人话筒
			myVid=document.getElementById("proposer,"+$scope.id+"Video");
			myVid.muted=true; 
		}else if(json.proOpenOwn!=null){
			//自己打开申请人话筒
			myVid=document.getElementById("proposer,"+$scope.id+"Video");
			myVid.muted=false;
		}else if(json.repCloseOwn!=null){ 
			
		}else if(json.repOpenOwn!=null){
			
		}else if(json.recCloseOwn!=null){
			myVid=document.getElementById("recorder,"+$scope.id+"Video");
			myVid.muted=true;  
		}else if(json.recOpenOwn!=null){
			myVid=document.getElementById("recorder,"+$scope.id+"Video");
			myVid.muted=false;  
		}else if(json.event=="role"){
			var role_to=json.data;
			con=connections[role_to];
			con.pc.close();
			
		}else{											//建立视频会议事件
			console.log("使用websokect传输在线视频信息")
			var con;
			//发送对象
			if(connections[json.owner])con=connections[json.owner];
			
			//如果是一个ICE的候选，则将其加入到PeerConnection中，否则设定对方的session描述为传递过来的描述
			if (json.event === "_ice_candidate") {
				con.pc.addIceCandidate(new RTCIceCandidate(json.data.candidate));
			} else if(json.event==="msg"){
				if(json.data!=role){
					con=new Connection(role,socket);	
					con.to=json.data;
					con.pc.addStream($scope.stream);
					connections[json.data]=con;	
				}
			}else if(json.event==="rolelist"){
				memberin=json.data;
				for(var i=0;i<memberin.length;i++){
					if(memberin[i]==role)continue;
					var con=new Connection(role,socket);
					con.to=memberin[i];
					connections[con.to]=con;
					
				}
			}
			else{
				con.pc.setRemoteDescription(new RTCSessionDescription(
						json.data.sdp));
				// 如果是一个offer，那么需要回复一个answer
				if (json.event === "_offer") {
					//_offer
					con.pc.createAnswer(con.sendAnswerFn, function(error) {
					});
				} else {
					//_answer
				}
			}				
		}

	};


	// 获取本地音频和视频流
	var promise=navigator.getUserMedia({
        audio: true,
        video: true
	},function(stream) {
		  /* use the stream */
        document.getElementById(role+'Video').src = window.URL.createObjectURL(stream);
        myVid=document.getElementById(role+"Video");
    	myVid.muted=true;
    	$scope.stream=stream;
        for(var i=0;i<roles.length;i++){     //排除了自己
        	if(!connections[roles[i]])continue;
        	var con=connections[roles[i]];
            //向PeerConnection中加入需要发送的流
            con.pc.addStream(stream);
            //如果是发起方则发送一个offer信令
            if(memberin.indexOf(con.to)>=0){
            	con.start();
            }            	
        }
		},function(err) {
	         console.log("The following error occurred: " + err.name);
	      });
   
})